package com.hs.sfzy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Course;
import com.hs.sfzy.entity.CourseDTO;
import com.hs.sfzy.entity.Coursemark;
import com.hs.sfzy.entity.Heartknowledge;
import com.hs.sfzy.service.CourseService;
import com.hs.sfzy.service.HeartknowledgeService;
import com.hs.sfzy.util.Result;
import com.hs.sfzy.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: sfzyBackEnd
 * @BelongsPackage: com.hs.sfzy.controller
 * @ClassName CourseController
 * @Author: 陈千鱼不是鱼
 * @CreateTime: 2024-10-24  14:32
 * @Description: 课程控制层
 * @Version: 1.0
 */
@RestController
@RequestMapping("/course")
@Api(tags = "课程")
@RequiredArgsConstructor
public class CourseController {


    private final CourseService courseService;

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result page(
           CourseDTO courseDTO
    ) {
        PageInfo<Course> info = courseService.page(courseDTO);

        return Result.success(info);
    }

    @PostMapping("/save")
    @ApiOperation("新增课程")
    public Result save(@RequestBody Course course) {
        courseService.save(course);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除课程")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            courseService.deleteById(id);
        }catch (Exception  e){
            return Result.error(e.getMessage());
        }

        return Result.success();
    }

    @PutMapping("/update")
    @ApiOperation("更新课程")
    public Result update(@RequestBody Course course) {
        courseService.update(course);
        return Result.success();
    }


    /**
     * 微信在线课程多条件查询
     * @author QinKunPeng
     * @date 2024/10/29 20:30
     * @param pageNum
     * @param pageSize
     * @param map
     * @return java.lang.Object
     */
    @GetMapping("/forwx")
    public Object selectForWx(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
            @RequestParam(required = false)Map map){

        PageHelper.startPage(pageNum, pageSize);
        List<Course> courses = courseService.selectForWx(map);
        PageInfo<Course> pageInfo = new PageInfo<>(courses);
        return ResultUtil.success().setData(pageInfo);
    }

    @GetMapping("/findById")
    public Result selectById(long id){
        Course course = courseService.selectById(id);
        return Result.success(course);
    }
}
